package ext.mango.base;

import javax.servlet.http.HttpSession;

import ext.mango.aa.eo.EOAAUser;
import ext.mango.functions.Func;
import mango.orm.DaoFactory;
import mango.web.Action;

public abstract class POBase extends Action {
    private boolean superUserInit = false;
    private boolean isSuperUser;
    
    public EOAAUser getCurrentUser() {
        return (EOAAUser) this.getSessionAttr(EOAAUser.class.getName());
    }
    
    public void setCurrentUser(EOAAUser usr) {
        this.setSessionAttr(EOAAUser.class.getName(), usr);
    }
    
    public String getUserId(){
        return this.getCurrentUser().getId();
    }
    
    public String getLanguage() {
        return Func.getLanguage(this.getRequest());
    }
    
    public HttpSession getSession() {
        return this.getRequest().getSession();
    }
    
    public boolean isSuperUser(){
        if(!superUserInit){
            String chkSql = "select 1 from AAUserToUserGroup where userId=? and usergroupId='Super'";
            String isSuper = DaoFactory.c(chkSql).setParameter(this.getCurrentUser().getId()).getStringValue();
            if("1".equals(isSuper)) {
                this.isSuperUser = true;
            }
            else {
                this.isSuperUser = false;
            }
            this.superUserInit = true;
        }
        return this.isSuperUser;
    }
    
    public boolean isSuperUser(String userID){
        String chkSql = "select 1 from AAUserToUserGroup where userId=? and usergroupId='Super'";
        String isSuper = DaoFactory.c(chkSql).setParameter(userID).getStringValue();
        if("1".equals(isSuper)) {
            return true;
        }
        return false;
    }
}
